home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / edit / elv18src.zip / prsvunix.c < prev    next >
C/C++ Source or Header  |  1993-10-15  |  3KB  |  134 lines

  1. /* prsvunix.c */
  2.  
  3. /* This file contains the UNIX-specific parts of the "elvprsv" program. */
  4.  
  5. #if OSK
  6. # define ELVPRSV
  7. # include "osk.c"
  8. #else
  9. # include <sys/stat.h>
  10. # include <pwd.h>
  11. #endif
  12. #ifndef __STDC__
  13. /* some older systems don't declare this in pwd.h, I guess. */
  14. extern struct passwd *getpwuid();
  15. #endif
  16.  
  17. /* This variable is used to add extra error messages for mail sent to root */
  18. char *ps;
  19.  
  20. /* This function returns the login name of the owner of a file */
  21. char *ownername(filename)
  22.     char    *filename;    /* name of a file */
  23. {
  24.     struct stat    st;
  25.     struct passwd    *pw;
  26.  
  27.     /* stat the file, to get its uid */
  28.     if (stat(filename, &st) < 0)
  29.     {
  30.         ps = "stat() failed";
  31.         return "root";
  32.     }
  33.  
  34.     /* get the /etc/passwd entry for that user */
  35.     pw = getpwuid(st.st_uid);
  36.     if (!pw)
  37.     {
  38.         ps = "uid not found in password file";
  39.         return "root";
  40.     }
  41.  
  42.     /* return the user's name */
  43.     return pw->pw_name;
  44. }
  45.  
  46.  
  47. /* This function sends a mail message to a given user, saying that a file
  48.  * has been preserved.
  49.  */
  50. void mail(user, file, when, tmp)
  51.     char    *user;    /* name of user who should receive the mail */
  52.     char    *file;    /* name of original text file that was preserved */
  53.     char    *when;    /* description of why the file was preserved */
  54.     char    *tmp;    /* NULL normally; else name of tmp file if user should run elvprsv -R */
  55. {
  56.     char    cmd[80];/* buffer used for constructing a "mail" command */
  57.     FILE    *m;    /* stream used for giving text to the "mail" program */
  58.     char    *base;    /* basename of the file */
  59.  
  60.     /* separate the directory name from the basename. */
  61.     for (base = file + strlen(file); --base > file && *base != SLASH; )
  62.     {
  63.     }
  64.     if (*base == SLASH)
  65.     {
  66.         *base++ = '\0';
  67.     }
  68.  
  69.     /* for anonymous buffers, pretend the name was "foo" */
  70.     if (!strcmp(base, "*"))
  71.     {
  72.         base = "foo";
  73.     }
  74.  
  75.     /* open a pipe to the "mail" program */
  76. #if OSK
  77.     sprintf(cmd, "%s \"-s=%s preserved!\" %s", MAILER, base, user);
  78. #else /* ANY_UNIX */
  79.     sprintf(cmd, "%s -s Graceland %s", MAILER, user);
  80.     switch (fork())
  81.     {
  82.       case -1: /* error */
  83.         return;
  84.  
  85.       case 0: /* child */
  86.         break; /* continue with the rest of this function */
  87.  
  88.       default: /* parent */
  89.         wait(NULL);
  90.         return;
  91.     }
  92. #endif
  93.     m = popen(cmd, "w");
  94.     if (!m)
  95.     {
  96.         perror(cmd);
  97.         /* Can't send mail!  Hope the user figures it out. */
  98.         return;
  99.     }
  100.  
  101.     /* Tell the user that the file was preserved */
  102.     fprintf(m, "A version of your file \"%s%c%s\"\n", file, SLASH, base);
  103.     fprintf(m, "was preserved when %s.\n", when);
  104.     fprintf(m, "To recover this file, do the following:\n");
  105.     fprintf(m, "\n");
  106. #if OSK
  107.     fprintf(m, "     chd %s\n", file);
  108. #else /* ANY_UNIX */
  109.     fprintf(m, "     cd %s\n", file);
  110. #endif
  111.     if (tmp)
  112.     {
  113.         fprintf(m, "     elvprsv %s\n", tmp);
  114.     }
  115.     else
  116.     {
  117.         fprintf(m, "     elvrec %s\n", base);
  118.     }
  119.     fprintf(m, "\n");
  120.     fprintf(m, "With fond wishes for a speedy recovery,\n");
  121.     fprintf(m, "                                    Elvis\n");
  122.     if (ps)
  123.     {
  124.         fprintf(m, "\nP.S. %s\n", ps);
  125.         ps = (char *)0;
  126.     }
  127.  
  128.     /* close the stream */
  129.     pclose(m);
  130. #if ANY_UNIX
  131.     exit(0);
  132. #endif
  133. }
  134.